package OOProgram;

public class Person {
    //    private String[] name;
    protected int age;
    protected String name;

    //        public static  int count = 0;//静态字段
//        public static int getCount() {
//            return Person.count;
//        }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Person) {
            Person other = (Person) obj;
            return this.name.equals(other.name) && this.age == other.age;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.name.hashCode() + this.age;
    }

    //    public String getName() {
//        return name[0]+' '+name[1];//如果没有名字冲突，可以省略this
//    }
    public Person(String name1, String name2) {
        this.name = name1 + ' ' + name2;
    }

    public Person(String name, int age) {
        this.setNameAndAge(name, age);
    }

    public Person() {
        this.setNameAndAge("unknow", 18);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name == null || name.isBlank()) {
            throw new IllegalArgumentException("名字不能为空");
        }
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        if (age < 0 || age > 200) {
            throw new IllegalArgumentException("年龄超出范围");
        }
        this.age = age;
    }

    public void setNameAndAge(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }
}
